package com.dearbinge.data.service.impl.moments;

import com.dearbinge.data.api.moments.BusinessCircleCommentService;
import com.dearbinge.data.dao.UserMapper;
import com.dearbinge.data.dao.moments.BusinessCircleCommentMapper;
import com.dearbinge.data.pojo.moments.vo.BusinessCircleCommentVO;
import com.dearbinge.data.pojo.user.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 社交圈评论业务层
 *
 * @author wes
 * @date 2019/2/25
 */
@Service("businessCircleCommentService")
public class BusinessCircleCommentServiceImpl implements BusinessCircleCommentService {

    @Autowired
    private BusinessCircleCommentMapper businessCircleCommentMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 删除
     *
     * @param id id
     * @return 结果
     */
    @Override
    public Integer delete(Integer id) {
        return businessCircleCommentMapper.deleteById(id);
    }

    /**
     * 更新
     *
     * @param comment 评论表
     * @return 结果
     */
    @Override
    public Integer update(BusinessCircleCommentVO comment) {
        return businessCircleCommentMapper.update(comment);
    }

    /**
     * 插入
     *
     * @param comment 评论表
     * @return 结果
     */
    @Override
    public Integer insert(BusinessCircleCommentVO comment) {
        return businessCircleCommentMapper.insert(comment);
    }

    /**
     * 根据id查评论回复
     *
     * @param tableId id
     * @return 评论集
     */
    @Override
    public List<BusinessCircleCommentVO> listCommentById(Integer tableType,Integer tableId) {
        List<BusinessCircleCommentVO> list = businessCircleCommentMapper.listCommentById(tableType,tableId);

        for (BusinessCircleCommentVO businessCircleCommentVO : list) {
            Integer contentUserId = businessCircleCommentVO.getContentUser();
            //根据id查名字
            User contentUser = userMapper.getById(contentUserId);
            if (contentUser != null) {
                businessCircleCommentVO.setContentUserName(contentUser.getName());
            }
            Integer replyUserId = businessCircleCommentVO.getReplyUser();
            if (replyUserId != null){
                User replyUser = userMapper.getById(replyUserId);
                businessCircleCommentVO.setReplyUserName(replyUser.getName());
            }
        }
        return list;
    }

}
